package com.syqc.monitor.ui.tyre

import com.gyf.immersionbar.ktx.immersionBar
import com.syqc.comlib.base.BaseActivity
import com.syqc.custom.TyreView
import com.syqc.monitor.databinding.ActivityTyreBinding
import com.syqc.utils.DataUtil
import com.syqc.utils.Keys

/**
 *@author joker
 *@time   2024/3/4 9:12
 *@desc
 */
class TyreInfoActivity : BaseActivity<ActivityTyreBinding>(ActivityTyreBinding::inflate) {
    private var type: Int = 0
    private var isTempC: Boolean = true

    private val mPopup by lazy {
        TyrePopup(this) { type, isTempC ->
            this.type = type
            this.isTempC = isTempC
            handleData()
        }
    }

    override fun initView() {
        binding.tvTyreTitle.text = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        immersionBar { titleBar(binding.mToolBarFast) }
        type = intent.getIntExtra(Keys.INTENT_TYPE, 0)
        isTempC = intent.getBooleanExtra(Keys.INTENT_OTHER, true)
        handleData()
    }

    private fun handleData() {
        DataUtil.psiTyres?.forEach {
            when (it[0]) {
                "01" -> setTyre(it, binding.tvTyre1)
                "02" -> setTyre(it, binding.tvTyre2)
                "03" -> setTyre(it, binding.tvTyre3)
                "04" -> setTyre(it, binding.tvTyre4)
                "05" -> setTyre(it, binding.tvTyre5)
                "06" -> setTyre(it, binding.tvTyre6)
                "07" -> setTyre(it, binding.tvTyre7)
                "08" -> setTyre(it, binding.tvTyre8)
                "09" -> setTyre(it, binding.tvTyre9)
                "10" -> setTyre(it, binding.tvTyre10)
                "11" -> setTyre(it, binding.tvTyre11)
                "12" -> setTyre(it, binding.tvTyre12)
                "13" -> setTyre(it, binding.tvTyre13)
                "14" -> setTyre(it, binding.tvTyre14)
                "15" -> setTyre(it, binding.tvTyre15)
                "16" -> setTyre(it, binding.tvTyre16)
                "17" -> setTyre(it, binding.tvTyre17)
                "18" -> setTyre(it, binding.tvTyre18)
                "19" -> setTyre(it, binding.tvTyre19)
                "20" -> setTyre(it, binding.tvTyre20)
                "21" -> setTyre(it, binding.tvTyre21)
                "22" -> setTyre(it, binding.tvTyre22)
            }

        }
    }

    private fun setTyre(it: List<String>, tyreView: TyreView) {
        val temp = if (isTempC) "${it[2]} ℃" else "${getC2F(it[2].toInt())} ℉"
        tyreView.showPsi(true)
        tyreView.setTyrePsi(changPressure(it[1], type))
        tyreView.setTyreTemp(temp)
    }


    override fun setListener() {
        binding.ivTyreStart.setOnClickListener {
            if (isHome) {
                startHomeActivity()
            }
            finish()
        }
        binding.ivTyreSetting.setOnClickListener {
            mPopup.showLocation(window.decorView, isTempC, type)
        }
    }

    /**
     * °C   to  °F
     *
     * @param tempC
     * @return
     */
    private fun getC2F(tempC: Int): Int = (tempC * 9 / 5) + 32

    /**
     *胎压转换
     * @param value
     * @param type
     */
    private fun changPressure(value: String, type: Int): String {
        val valuef = value.toDouble()
        return when (type) {
            1 -> "${String.format("%.2f", valuef * 145 / 1000.0)} \nPsi"
            2 -> "${String.format("%.2f", valuef * 102 / 10000.0)} \nkg/cm2"
            3 -> "${String.format("%.2f", valuef / 100.0)} \nbar"
            else -> "${String.format("%.2f", valuef)} \nKpa"
        }
    }
}